<?php
class HaiboClass {
	
	/**
	 * 
	 * 生成插入数据
	 * @param String $table
	 * @param Array $arr
	 * @return String $sql
	 */
	public function insert($table, $arr) {
		if (is_array ( $arr )) {
			foreach ( $arr as $key => $value ) {
				$keys [] = "`" . $key . "`";
				$values [] = "'" . $value . "'";
			}
			
			$keystr = implode ( ",", $keys );
			$contenstr = implode ( ",", $values );
			
			return $sql = "insert into $table ($keystr)values($contenstr)";
		}
	}
	
	/**
	 * 
	 * 生成随机密码
	 * @param String $len
	 * @param Sring $format
	 */
	public function randstr($len = 6, $format = 'NUMBER') {
		switch ($format) {
			case 'ALL' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
				break;
			case 'password' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
				break;
			case 'CHAR' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~';
				break;
			case 'NUMBER' :
				$chars = '0123456789';
				break;
			default :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
				break;
		}
		mt_srand ( ( double ) microtime () * 1000000 * getmypid () );
		$password = '';
		
		while ( strlen ( $password ) < $len )
			$password .= substr ( $chars, (mt_rand () % strlen ( $chars )), 1 );
		return $password;
	}
	
	/**
	 * 
	 * 去除不要的数组
	 * @param  Array $arr  要处理数据
	 * @param Array $delarr  要删除的数组
	 */
	public function delArr($arr, $delarr) {
		if (is_array ( $arr )) {
			foreach ( $arr as $key => $value ) {
				foreach ( $delarr as $delkey => $delval ) {
					if ($key == $delval) {
						unset ( $arr [$key] );
					}
				}
			}
		}
		return $arr;
	}
	
	public function update($table, $arr, $wherearr) {
		
		if (is_array ( $arr )) {
			foreach ( $arr as $key => $value ) {
				$newarr [] = "`" . $key . "` = '" . $value . "'";
			}
		}
		
		if (is_array ( $wherearr )) {
			foreach ( $wherearr as $key => $value ) {
				$newwherearr [] = "`" . $key . "` = '" . $value . "'";
			}
		}
		
		$str = implode ( ",", $newarr );
		$wherestr = implode ( ",", $newwherearr );
		
		return $sql = " update " . $table . " set " . $str . " where " . $wherestr;
	}

}



?>